/**
 * Created by duzi on 7/2/15.
 */
$(document).ready(function(){
    var $genralSearchInput = $('.genralSearchInput');
    var $customer = $('.customer');
    var $select = $('#opt');
    var $billStart = $('.searchBill .start')
    var $billEnd = $('.searchBill .end');
    var $category = $('.category');
    var $start = $('.searchSell .start')
    var $end = $('.searchSell .end')
    $genralSearchInput.on('keyup', function(event) {
        if (event.keyCode == 13 && this.value && $select.val()) {
            genralSearch(this.value, $select.val())
        }
    })

    $customer.on('keyup', function(event){
        this.value = this.value.replace(/[^\d]/g,'')

        if(!this.value) return;

        if(event.keyCode == 13 && this.value &&  $billStart.val()){
            billSearch(this.value, $billStart.val(), $billEnd.val())
            return;
        }
        findCustomer(this.value);
    })

    $category.on('keyup', function(event){
        if(event.keyCode == 13 && this.value && $start.val()){
            sellSearch(this.value, $start.val() , $end.val());
        }
    })

    function genralSearch(a, b){
        $.ajax({
            type: "GET",
            url: '/'+ b + '/' + a,
            contentType: 'application/json',
            success: function(response){
                console.log(response)
            }
        })
    }


    function billSearch(a, b, c){
        $.ajax({
            type: "GET",
            url: '/bills/' + a + '/' + b +'/'+c,
            contentType: 'application/json',
            success: function(response){
                console.log(response)
            }
        })
    }

    function sellSearch(a, b, c){
        $.ajax({
            type: "GET",
            url: '/bills/' + a + '/' + b + '/' + c,
            contentType: 'application/json',
            success: function(response){
                console.log(response)
            }
        })
    }

    function findCustomer(id){
        $.ajax({
            type: "GET",
            url: '/customers/' + id,
            contentType: 'application/json',
            success: function(response){
                console.log(response)
            }
        })
    }

});